<!DOCTYPE html>
<html>
<!--
Copyright 2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
  <title>Google Earth API Samples - Playing Tours + Slider</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <!-- You should probably remove most of this extra cruft if you're copying
       this sample! -->
  <style type="text/css">@import "static/examples.css";</style>
  <style type="text/css">@import "static/prettify/prettify.css";</style>
  <script type="text/javascript" src="static/prettify/prettify.js"></script>
  
  <!-- Change the key below to your own Maps API key -->
  <script type="text/javascript" src="http://www.google.com/jsapi?hl=en&amp;key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
<!-- We'll need to walk the DOM looking for a KmlTour later -->
<script src="http://earth-api-samples.googlecode.com/svn/trunk/lib/kmldomwalk.js" type="text/javascript"></script>
<!-- Very basic slider -->
<script src="static/simple-slider.js" type="text/javascript"></script>
<link href="static/simple-slider.css" type="text/css" rel="stylesheet"/>
  <script type="text/javascript">
  /* <![CDATA[ */
  var ge;
  google.load("earth", "1");

  function init() {
    google.earth.createInstance('map3d', initCB, failureCB);
  }
  
  function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

    // create the tour by fetching it out of a KML file
    var href = window.location.href.substring(0, window.location.href.lastIndexOf('/')) + '/';
    href += 'static/grandcanyon_tour.kmz';
    
    google.earth.fetchKml(ge, href, function(kmlObject) {
      if (!kmlObject) {
        // wrap alerts in API callbacks and event handlers
        // in a setTimeout to prevent deadlock in some browsers
        setTimeout(function() {
          alert('Bad or null KML.');
        }, 0);
        return;
      }
    
      // Show the entire KML file in the plugin.
      ge.getFeatures().appendChild(kmlObject);
    
      // Walk the DOM looking for a KmlTour
      walkKmlDom(kmlObject, function() {
        if (this.getType() == 'KmlTour') {
          ge.getTourPlayer().setTour(this);
          return false; // stop the DOM walk here.
        }
      });
      
      // make the slider for dragging the tour timeline
      var slider = new SimpleSlider(document.getElementById('slider-container'), {
        onSlide: function(pos) {
          ge.getTourPlayer().setCurrentTime(pos);
        },
        max: ge.getTourPlayer().getDuration(),
        formatPosFn: formatTime
      });
    
      window.setInterval(function() {
        slider.setPosition(ge.getTourPlayer().getCurrentTime());
      }, 50);
    });
    
    document.getElementById('installed-plugin-version').innerHTML =
      ge.getPluginVersion().toString();
  }
  
  function failureCB(errorCode) {
  }
  
  function playTour() {
    ge.getTourPlayer().play();
  }
  
  function pauseTour() {
    ge.getTourPlayer().pause();
  }
  
  function resetTour() {
    ge.getTourPlayer().reset();
  }
  
  function formatTime(sec) {
    var pad2 = function(n) {
      return (n < 10) ? '0' + n : n;
    };
  
    sec = Math.floor(sec);
  
    var min = Math.floor(sec / 60);
    sec = sec % 60;
  
    var hrs = Math.floor(min / 60);
    min = min % 60;
  
    var str = '';
    str += hrs
  
    return (hrs ? (hrs + ':' + pad2(min)) : min) + ':' + pad2(sec);
  }
  
  /* ]]> */
  </script>
</head>
<body onload="if(window.prettyPrint)prettyPrint();init();">
  <h1>Google Earth API Samples - Playing Tours + Slider</h1>
  <dl>
            <dt>Last Modified:</dt><dd>06/08/2009</dd>
    <dt>Installed Plugin Version:</dt><dd id="installed-plugin-version">...</dd>
  </dl>
  <div style="clear: both;"></div>
  
  <div id="ui" style="position: relative;">
    <div id="map3d" style="border: 1px solid silver; width: 500px; height: 500px;"></div>

    <div id="extra-ui" style="position: absolute; left: 520px; top: 0;">
      <input type="button" onclick="playTour()" value="Play Tour"/>
      <input type="button" onclick="pauseTour()" value="Pause Tour"/>
      <input type="button" onclick="resetTour()" value="Stop/Reset Tour"/>
      <div id="slider-container"></div>
      
      <h2>Relevant Resources:</h2>
      <ul>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_g_e_tour_player.html">GETourPlayer Reference</a></li>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_kml_tour.html">KmlTour Reference</a></li>
<li><a href="http://code.google.com/apis/earth/documentation/reference/interface_g_e_plugin.html">GEPlugin Reference</a></li>
      </ul>
      <h2>Relevant Code Excerpt:</h2>      <pre class="prettyprint lang-html">&lt;!-- We'll need to walk the DOM looking for a KmlTour later --&gt;
&lt;script src=&quot;http://earth-api-samples.googlecode.com/svn/trunk/lib/kmldomwalk.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;!-- Very basic slider --&gt;
&lt;script src=&quot;static/simple-slider.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;static/simple-slider.css&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;/&gt;</pre>      <pre class="prettyprint lang-js">// create the tour by fetching it out of a KML file
var href = window.location.href.substring(0, window.location.href.lastIndexOf('/')) + '/';
href += 'static/grandcanyon_tour.kmz';

google.earth.fetchKml(ge, href, function(kmlObject) {
  if (!kmlObject) {
    // wrap alerts in API callbacks and event handlers
    // in a setTimeout to prevent deadlock in some browsers
    setTimeout(function() {
      alert('Bad or null KML.');
    }, 0);
    return;
  }

  // Show the entire KML file in the plugin.
  ge.getFeatures().appendChild(kmlObject);

  // Walk the DOM looking for a KmlTour
  walkKmlDom(kmlObject, function() {
    if (this.getType() == 'KmlTour') {
      ge.getTourPlayer().setTour(this);
      return false; // stop the DOM walk here.
    }
  });
  
  // make the slider for dragging the tour timeline
  var slider = new SimpleSlider(document.getElementById('slider-container'), {
    onSlide: function(pos) {
      ge.getTourPlayer().setCurrentTime(pos);
    },
    max: ge.getTourPlayer().getDuration(),
    formatPosFn: formatTime
  });

  window.setInterval(function() {
    slider.setPosition(ge.getTourPlayer().getCurrentTime());
  }, 50);
});</pre>      <pre class="prettyprint lang-js">function playTour() {
  ge.getTourPlayer().play();
}

function pauseTour() {
  ge.getTourPlayer().pause();
}

function resetTour() {
  ge.getTourPlayer().reset();
}

function formatTime(sec) {
  var pad2 = function(n) {
    return (n &lt; 10) ? '0' + n : n;
  };

  sec = Math.floor(sec);

  var min = Math.floor(sec / 60);
  sec = sec % 60;

  var hrs = Math.floor(min / 60);
  min = min % 60;

  var str = '';
  str += hrs

  return (hrs ? (hrs + ':' + pad2(min)) : min) + ':' + pad2(sec);
}</pre>    </div>
  </div>
</body>
</html>
